/**
 * Created by bb on 16/9/19.
 */

import * as types from '../action/actionTypes';
import merge from 'lodash/merge';
import assign from 'lodash/assign';

const initialState = {
    loading: false,
    deviceInfo: []
};

export default function deviceReducer(state = initialState, action) {
    switch (action.type) {
        case types.DEVICE_LIST:
        //     const device = action.device;
        //     const item = { device.deviceId: device };
		//
        // const deviceInfo = assign({}, state.deviceInfo, item);
        // const state = assing({}, state, { deviceInfo })
        //     console.warn('state.deviceInfo111' + JSON.stringify(state));
            return assign({}, state, {
                loading: true,
                deviceInfo: action.deviceInfo
            });
            break;
        case types.UPDATE_DEVICE_LIST:
            var deviceInfoNew = [];
            state && state.deviceInfo && state.deviceInfo.forEach((ele)=> {
                if (ele.deviceId == action.device.deviceId) {

                    const tempobj = assign({}, ele, action.device);
                    deviceInfoNew.push(tempobj);
                }else{
                    deviceInfoNew.push(ele);
                }
            });
            return assign({}, state, {
                loading: true,
                deviceInfo: deviceInfoNew
            });
            // return newDevices;
            break;
        default:
            return state;
    }

}

